package cn.highset.modules.base.controller.manage;

import cn.highset.common.utils.PageUtil;
import cn.highset.common.utils.ResultUtil;
import cn.highset.common.vo.PageVo;
import cn.highset.common.vo.Result;
import cn.highset.common.vo.SearchVo;
import cn.highset.modules.base.entity.Banner;
import cn.highset.modules.base.factory.BannerFactory;
import cn.highset.modules.base.service.BannerService;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * @author Victor
 */
@Slf4j
@RestController
@Api(description = "轮播图表管理接口")
@RequestMapping("/highset/banner")
@Transactional
public class BannerController {

    @Autowired
    private BannerService bannerService;
    @Autowired
    private StringRedisTemplate redisTemplate;

    @RequestMapping(value = "/getByCondition", method = RequestMethod.GET)
    @ApiOperation(value = "多条件分页获取")
    public Result<Page<Banner>> getByCondition(@ModelAttribute Banner banner,
                                               @ModelAttribute SearchVo searchVo,
                                               @ModelAttribute PageVo pageVo) {

        Page<Banner> page = bannerService.findByCondition(banner, searchVo, PageUtil.initPage(pageVo));
        return new ResultUtil<Page<Banner>>().setData(page);
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(value = "添加轮播图")
    public Result<Object> add(@ModelAttribute Banner b) {
        if (StrUtil.hasBlank(b.getTitle(), b.getImageUrl(), b.getEndpoint())) {
            return new ResultUtil<Object>().setErrorMsg("缺少必需表单字段");
        }

        Banner banner = bannerService.save(b);
        if (banner == null) {
            return new ResultUtil<Object>().setErrorMsg("添加失败");
        }
        //清除小程序首页访问缓存
        boolean bool = redisTemplate.delete("banner::home");
        return new ResultUtil<Object>().setSuccessMsg("添加成功");
    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ApiOperation(value = "修改轮播图", notes = "需要通过id获取原轮播图信息")
    public Result<Object> edit(@ModelAttribute Banner b) {
        if (StrUtil.hasBlank(b.getId(), b.getTitle(), b.getImageUrl(), b.getEndpoint())) {
            return new ResultUtil<Object>().setErrorMsg("缺少必需表单字段");
        }
        Banner old = bannerService.get(b.getId());

        Banner banner = bannerService.update(BannerFactory.edit(b, old));

        if (banner == null) {
            return new ResultUtil<Object>().setErrorMsg("修改失败");
        }
        //清除小程序首页访问缓存
        redisTemplate.delete("banner::home");
        return new ResultUtil<Object>().setSuccessMsg("修改成功");
    }

    @RequestMapping(value = "/delByIds/{ids}", method = RequestMethod.DELETE)
    @ApiOperation(value = "批量通过ids删除", notes = "逻辑删除")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(paramType = "path", name = "ids", value = "轮播图ID数组", dataType = "String", required = true, allowMultiple = true),
    })
    public Result<Object> delAllByIds(@PathVariable String[] ids) {
        for (String id : ids) {
            bannerService.delete(id);
        }
        //清除小程序首页访问缓存
        redisTemplate.delete("banner::home");
        return new ResultUtil<Object>().setSuccessMsg("批量通过id删除数据成功");
    }


}
